package com.swz.dcrm.ui.approval;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.swz.dcrm.adpter.FragmentViewPagerAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Tab;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.car.CarResItemFragment;
import com.swz.dcrm.ui.viewmodel.ApprovalViewModel;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ApprovalRecordFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tab)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private List<String> titles;
    private List<Fragment> fragments;
    @Inject
    CarManagementViewModel carManagementViewModel;
    @Inject
    ApprovalViewModel approvalViewModel;
    public static ApprovalRecordFragment newInstance() {

        return new ApprovalRecordFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        approvalViewModel.getApprovalTabsResult(true).setValue(null);
        approvalViewModel.getApprovalTabs(true)
                .observe(this, new Observer<BaseResponse<List<Tab>>>() {
                    @Override
                    public void onChanged(BaseResponse<List<Tab>> listBaseResponse) {
                        if(listBaseResponse==null){
                            return;
                        }
                        approvalViewModel.getApprovalTabsResult(true).removeObserver(this);
                        if (listBaseResponse.isSuccess()) {
                            List<Tab> carTabs = listBaseResponse.getData();
                            fragments = new ArrayList<>();
                            titles = new ArrayList<>();
                            for(Tab carTab:carTabs){
                                titles.add(carTab.getName()+"("+carTab.getTypeCount()+")");
                                fragments.add(ApprovalItemFragment.newInstance(carTab.getCode(),true));
                            }
                            viewPager.setAdapter(new FragmentViewPagerAdapter(getChildFragmentManager(),fragments, titles));

                            slidingTabLayout.setViewPager(viewPager);
                        }
                    }
                });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public int layoutId() {
        return R.layout.fragment_approval_record;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.approval_record));
        viewPager.setOffscreenPageLimit(4);
        onLoadRetry();
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
